# app/errors.py
from flask import jsonify
from werkzeug.exceptions import HTTPException

def init_error_handlers(app):
    @app.errorhandler(400)
    def bad_request(e):
        return jsonify(
            status="error",
            message="请求参数错误",
            details=str(e.description) if hasattr(e, 'description') else str(e)
        ), 400

    @app.errorhandler(404)
    def not_found(e):
        return jsonify(
            status="error",
            message="资源不存在",
            details=str(e.description)
        ), 404

    @app.errorhandler(Exception)
    def global_error(e):
        code = 500
        print(e)
        if isinstance(e, HTTPException):
            code = e.code
        return jsonify(
            status="error",
            message=str(e),
            type=e.__class__.__name__
        ), code